package cn.tjd.smart_health.admin.controller;

import cn.tjd.smart_health.api.SetMealService;
import cn.tjd.smart_health.common.constant.MessageConstant;
import cn.tjd.smart_health.common.entity.Setmeal;
import cn.tjd.smart_health.common.pojo.PageSelect;
import cn.tjd.smart_health.common.pojo.QueryBean;
import cn.tjd.smart_health.common.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @DubboReference
    SetMealService setMealService;

    @PostMapping("/upload")
    public R upload(@RequestParam("imgFile") MultipartFile file) throws IOException {
        if (file != null){
            byte[] data = file.getBytes();
            String fileName = file.getOriginalFilename();
            String img = setMealService.uploadImg(data , fileName);
            return new R(true, MessageConstant.PIC_UPLOAD_SUCCESS,img);
        }
        return new R(false, MessageConstant.PIC_UPLOAD_FAIL);
    }


    @PostMapping("/add")
    public R save(@RequestBody Setmeal setMeal ,
                  @RequestParam("checkgroupIds") Integer[] checkgroups) {
        try {
            setMealService.saveSetMeal(setMeal,checkgroups);
            return new R(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


    @PostMapping("/findPage")
    public PageSelect<Setmeal> findPage(@RequestBody QueryBean queryBean){

            return setMealService.findPage(queryBean.getCurrentPage(),
                    queryBean.getPageSize(),
                    queryBean.getQueryString());

    }

    @GetMapping("/findMealById")
    public R findMealById(@RequestParam("id") Integer id){
        try {
            Setmeal setmeal = setMealService.findById(id);
            return new R(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @GetMapping("/findCheckgroupIdsByMealId")
    public R findCheckgroupIdsByMealId(@RequestParam("id") Integer id){
        try {
            List<Integer> checkgroupIds = setMealService.findCheckgroupIdsByMealId(id);
            return new R(true, MessageConstant.QUERY_SETMEAL_SUCCESS,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @PostMapping("/edit")
    public R edit(@RequestBody Setmeal setMeal,@RequestParam("checkgroupIds") Integer[] checkGroups){
        try {
            setMealService.editSetMeal(setMeal,checkGroups);
            return new R(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @GetMapping("/delete")
    public R delete(@RequestParam("id") Integer id){
        try {
            setMealService.deleteSetMeal(id);
            return new R(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }



}
